package com.wisdom.benefit.jufa.widget;

import android.view.View;
import android.view.ViewStub;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.IntDef;

import com.wisdom.benefit.jufa.R;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

/**
 * ResultViewHelper
 * Created by zhoudawei on 2017/2/17.
 */

public class ResultViewHelper {

    private View mContainerView;
    private View mContentView;
    private ViewStub mDefaultViewStub;
    private View mDefaultView;
    public ImageView mIconView;
    public TextView mMessageView;

    private View.OnClickListener mOnClickListener;
    private int mResultType;

    public ResultViewHelper(View containerView, View contentView) {
        mContainerView = containerView;
        mContentView = contentView;
        mDefaultViewStub = mContainerView.findViewById(R.id.default_view_stub);
    }

    public void setOnClickListener(View.OnClickListener onClickListener) {
        mOnClickListener = onClickListener;
    }

    public void showResult(@resultType int resultType) {
        mResultType = resultType;
        switch (resultType) {
            case TYPE_HAS_CONTENT:
                showHasContentView();
                break;
            case TYPE_EMPTY_CONTENT:
                showEmptyContentView();
                break;
            case TYPE_CONNECT_SERVER_FAILURE:
                showConnectServerFailureView();
                break;
            case TYPE_NO_INTERNET:
                showNoInternetView();
                break;
        }
    }

    private void showHasContentView() {
        mContentView.setVisibility(View.VISIBLE);
        if (mDefaultView != null) {
            mDefaultView.setVisibility(View.GONE);
        }
    }

    private View.OnClickListener reClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (mResultType == TYPE_CONNECT_SERVER_FAILURE || mResultType == TYPE_NO_INTERNET) {
                if (mOnClickListener != null) {
                    mOnClickListener.onClick(v);
                }
            }
        }
    };

    private void init() {
        if (mDefaultView == null) {
            mDefaultViewStub.inflate();
            mDefaultView = mContainerView.findViewById(R.id.layout_request_result);
            mDefaultView.setOnClickListener(reClickListener);
        }
        if (mIconView == null) {
            mIconView = mContainerView.findViewById(R.id.result_icon_view);
        }
        if (mMessageView == null) {
            mMessageView = mContainerView.findViewById(R.id.result_message_view);
        }
    }

    private void showEmptyContentView() {
        mContentView.setVisibility(View.GONE);
        init();
        mIconView.setImageResource(R.drawable.ic_result_no_content);
        mMessageView.setText(R.string.result_no_content);
        mDefaultView.setVisibility(View.VISIBLE);
    }

    private void showConnectServerFailureView() {
        mContentView.setVisibility(View.GONE);
        init();
        mIconView.setImageResource(R.drawable.ic_result_request_failure);
        mMessageView.setText(R.string.result_request_failure);
        mDefaultView.setVisibility(View.VISIBLE);
    }

    private void showNoInternetView() {
        mContentView.setVisibility(View.GONE);
        init();
        mIconView.setImageResource(R.drawable.ic_result_no_internet);
        mMessageView.setText(R.string.result_no_internet);
        mDefaultView.setVisibility(View.VISIBLE);
    }

    public static final int TYPE_HAS_CONTENT = 0;
    public static final int TYPE_EMPTY_CONTENT = 1;
    public static final int TYPE_CONNECT_SERVER_FAILURE = 2;
    public static final int TYPE_NO_INTERNET = 3;

    @IntDef({TYPE_HAS_CONTENT, TYPE_EMPTY_CONTENT, TYPE_CONNECT_SERVER_FAILURE, TYPE_NO_INTERNET})
    @Retention(RetentionPolicy.SOURCE)
    @interface resultType {

    }
}
